package com.anke.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.anke.constant.MessageConstant;
import com.anke.entity.Company;
import com.anke.entity.Result;
import com.anke.service.CompanyService;
import com.github.pagehelper.PageHelper;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController// 和controller注解的不同在于其中的方法返回的都是json
@RequestMapping("/company")
@CrossOrigin
public class CompanyController {

    @Reference
    private CompanyService companyService;


    @RequestMapping("/register")
    public Result register(@RequestBody Company company){
        System.out.println(company);
        System.out.println("请求注册");
        try {
            companyService.register(company);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.REGISTER_FAIL);
        }
        return new Result(true, MessageConstant.REGISTER_SUCCESS);
    }

    @PostMapping("/login")
    public Result login(@RequestBody Company company){
        try {
            if (companyService.login(company)) {
                return new Result(true, MessageConstant.LOGIN_SUCCESS);
            }
            return new Result(false,MessageConstant.LOGIN_FAIL);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.SYSTEM_EXCEPTION);
        }
    }
    @RequestMapping("/selectAllCompany")
    public Result selectAllCompany() {
        try {
            PageHelper.startPage(1,2);
            List<Company> c = companyService.selectAllCompany();
            if (c.isEmpty()) {
                return new Result(false,MessageConstant.SELECT_FAIL);
            }
            return new Result(true, MessageConstant.SELECT_SUCCESS, c);
        } catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.SYSTEM_EXCEPTION);
        }
    }
    @RequestMapping("/selectCompanyByType")
    public Result selectCompanyByType(@RequestBody Company company) {
        try {
            PageHelper.startPage(1,2);
            List<Company> c = companyService.selectCompanyByType(company);
            if (c.isEmpty()) {
                return new Result(false,MessageConstant.SELECT_FAIL);
            }
            return new Result(true, MessageConstant.SELECT_SUCCESS, c);
        } catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.SYSTEM_EXCEPTION);
        }
    }
    @RequestMapping("/selectCompanyByName")
    public Result selectCompanyByName(@RequestBody Company company) {
        try {
            PageHelper.startPage(1,2);
            List<Company> c = companyService.selectCompanyByName(company);
            if (c.isEmpty()) {
                return new Result(false,MessageConstant.SELECT_FAIL);
            }
            return new Result(true, MessageConstant.SELECT_SUCCESS, c);
        } catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.SYSTEM_EXCEPTION);
        }
    }
    @RequestMapping("/selectCompanyByLocation")
    public Result selectCompanyByLocation(@RequestBody Company company) {
        try {
            PageHelper.startPage(1,2);
            List<Company> c = companyService.selectCompanyByLocation(company);
            if (c.isEmpty()) {
                return new Result(false,MessageConstant.SELECT_FAIL);
            }
            return new Result(true, MessageConstant.SELECT_SUCCESS, c);
        } catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.SYSTEM_EXCEPTION);
        }
    }
    @RequestMapping("/deleteCompany")
    public Result deleteCompany(@RequestBody Company company) {
        try {
            companyService.deleteCompany(company);
        } catch (Exception e) {
            return new Result(false, MessageConstant.DELETE_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_SUCCESS);
    }
    @RequestMapping("/updateCompany")
    public Result updateCompany(@RequestBody Company company) {
        try {
            companyService.updateCompany(company);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.UPDATE_FAIL);
        }
        return new Result(true, MessageConstant.UPDATE_SUCCESS);
    }
}
